package com.yanqu.road.server.gameplayer.module.activity.hsisland;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.hsisland.config.*;
import com.yanqu.road.entity.activity.hsisland.data.HsIslandPatronsData;
import com.yanqu.road.entity.enums.*;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroupUnion;
import com.yanqu.road.exception.BusinessException;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.activity.HsIslandProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.gameplayer.module.useract.UserActModule;
import com.yanqu.road.server.manger.VipMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.hsisland.HsIslandActivityMgr;
import com.yanqu.road.server.manger.activity.hsisland.pb.HsIslandActivityPb;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class HsIslandActivityModule extends GeneralModule {

    private static Logger logger = LogManager.getLogger(HsIslandActivityModule.class.getName());

    public HsIslandActivityModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadData() {
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    @Override
    public boolean saveData() {
        return true;
    }

    @Override
    public void afterLogin() {
        //判断系统是否解锁
        if (SystemOpenMgr.systemOpen(player, eSystemId.HsIsland.getValue())) {
            //推送活动配置
            this.syncConfig();
            //通知跨服，登录后处理
            this.crossAfterLoginHandle();
        }
    }

    /**
     * 跨服登录后处理
     */
    private void crossAfterLoginHandle() {
        for (HsIslandConfig config : HsIslandActivityMgr.getActivityConfigMap().values()) {
            HsIslandProto.HsIslandAfterLoginHandleCrossReqMsg.Builder msg = HsIslandProto.HsIslandAfterLoginHandleCrossReqMsg.newBuilder();
            msg.setActivityId(config.getActivityInfo().getActivityId());
            //上跨服
            player.sendUnionActivityGroupPacket(Protocol.C2_HS_ISLAND_AFTER_LOGIN_HANDLE, msg, msg.getActivityId());
        }
    }

    /**
     * 同步配置
     */
    public void syncConfig() {
        //判断系统是否解锁
        if (!SystemOpenMgr.systemOpen(player, eSystemId.HsIsland.getValue())) {
            return;
        }
        //同步配置
        Map<Integer, HsIslandConfig> configMap = HsIslandActivityMgr.getActivityConfigMap();
        for (HsIslandConfig config : configMap.values()) {
            if (!ActivityHelper.activityInShowTime(config.getActivityInfo())) {
                continue;
            }
            HsIslandProto.HsIslandSyncConfigMsg.Builder builder = HsIslandProto.HsIslandSyncConfigMsg.newBuilder();
            builder.setActivityId(config.getActivityInfo().getActivityId());
            //船配置
            for (HsIslandBoatConfig boatConfig :config.getBoatConfigMap().values()) {
                builder.addBoatConfigs(HsIslandActivityPb.buildHsIslandBoatConfig(boatConfig, player.getLanguage()));
            }
            //岛配置
            for (HsIslandIslandConfig itemConfig :config.getIslandConfigMap().values()) {
                builder.addIslandConfigs(HsIslandActivityPb.buildHsIslandIslandConfig(itemConfig));
            }
            //事件配置
            for (HsIslandEventConfig itemConfig :config.getEventConfigMap().values()) {
                builder.addEventConfigs(HsIslandActivityPb.buildHsIslandEventConfig(itemConfig, player.getLanguage()));
            }
            //pve配置
            for (HsIslandPveConfig itemConfig :config.getPveConfigMap().values()) {
                builder.addPveConfigs(HsIslandActivityPb.buildHsIslandPveConfig(itemConfig, player.getLanguage()));
            }
            //决斗配置
            for (HsIslandDuelConfig itemConfig :config.getDuelConfigMap().values()) {
                builder.addDuelConfigs(HsIslandActivityPb.buildHsIslandDuelConfig(itemConfig));
            }
            //宝箱配置
            for (HsIslandBoxConfig itemConfig :config.getBoxConfigMap().values()) {
                builder.addBoxConfigs(HsIslandActivityPb.buildHsIslandBoxConfig(itemConfig));
            }
            //2选1配置
            for (HsIslandChooseConfig itemConfig :config.getChooseConfigMap().values()) {
                builder.addChooseConfigs(HsIslandActivityPb.buildHsIslandChooseConfig(itemConfig, player.getLanguage()));
            }
            //buff配置
            for (HsIslandBuffConfig itemConfig :config.getBuffConfigMap().values()) {
                builder.addBuffConfigs(HsIslandActivityPb.buildHsIslandBuffConfig(itemConfig, player.getLanguage()));
            }
            //奇遇配置
            for (HsIslandRelicConfig itemConfig :config.getRelicConfigMap().values()) {
                builder.addRelicConfigs(HsIslandActivityPb.buildHsIslandRelicConfig(itemConfig));
            }
            //boss奖励配置
            for (HsIslandBossRewardConfig itemConfig :config.getBossRewardConfigMap().values()) {
                builder.addBossRewardConfigs(HsIslandActivityPb.buildHsIslandBossRewardConfig(itemConfig));
            }
            //圣柱馈赠
            for (HsIslandGiftConfig itemConfig :config.getGiftConfigMap().values()) {
                builder.addGiftConfigs(HsIslandActivityPb.buildHsIslandGiftConfig(itemConfig, player.getLanguage()));
            }
            //推送
            player.sendPacket(Protocol.U_HS_ISLAND_SYNC_CONFIG, builder);
        }
    }

    /**
     * 公用check
     * @param activityId
     * @param checkInTime
     * @throws BusinessException
     */
    public void commonCheck(int activityId, boolean checkInTime) throws BusinessException {
        //活动是否存在
        ActivityInfo activityInfo = ActivityMgr.getOpenActivityInfo(activityId);
        if (activityInfo == null) {
            throw new BusinessException(GameErrorCode.E_ACTIVITY_NO_FOUND);
        }
        //活动配置
        HsIslandConfig config = HsIslandActivityMgr.getActivityConfig(activityId);
        if (config == null) {
            throw new BusinessException(GameErrorCode.E_HS_ISLAND_ACTIVITY_NOT_FOUND);
        }
        //判断是否在活动展示期
        if (!ActivityHelper.activityInShowTime(activityInfo)) {
            throw BusinessException.newException(GameErrorCode.E_HS_ISLAND_ACTIVITY_NOT_IN_SHOW_TIME);
        }
        //判断是否在活动进行期
        if (checkInTime && !ActivityHelper.activityInTime(activityInfo)) {
            throw BusinessException.newException(GameErrorCode.E_HS_ISLAND_ACTIVITY_NOT_IN_TIME);
        }
        //系统是否解锁
        if (!SystemOpenMgr.systemOpen(player, eSystemId.HsIsland.getValue())) {
            throw BusinessException.newException(GameErrorCode.E_SYSTEM_NO_OPEN);
        }
        //判断玩家是否在名单内
        UnionActivityGroupUnion unionData = GameUnionActivityGroupMgr.getUnionData(activityId, player.getUserId());
        if (unionData == null) {
            throw BusinessException.newException(GameErrorCode.E_HS_ISLAND_NOT_IN_LIST);
        }
    }

    /**
     * 获取前100战力门客
     * @return
     */
    public List<HsIslandPatronsData> getSortPatronsList(int activityId) {
        List<HsIslandPatronsData> list = new ArrayList<>();
        //获取前100门客
        List<UserPatrons> patronsList = player.getModule(PatronsModule.class).getTopAbilityPatrons(100);
        //计算一下战力
        for (UserPatrons patrons : patronsList) {
            //计算战力
            long basePower = this.countPatronsPower(activityId, patrons.getAbility().longValue());
            HsIslandPatronsData data = new HsIslandPatronsData();
            data.setPatronsId(patrons.getPatronsId());
            data.setPower(basePower);
            list.add(data);
        }
        return list;
    }

    /**
     * 计算门客战力
     * @param activityId
     * @param ability
     * @return
     */
    public long countPatronsPower(int activityId, long ability) {
        HsIslandConfig config = HsIslandActivityMgr.getActivityConfig(activityId);
        List<Integer> paramList = config.HSISLAND_POWER_PARAM;
        //value1
        double value1 = Math.pow((double) ability / (double) paramList.get(0) + (double) paramList.get(2), (double) paramList.get(1));
        //value2
        int vipLevel = VipMgr.getVipLv(player.getUserInfo().getVipExp());
        double value2 = vipLevel * paramList.get(5) + paramList.get(4);
        //value3
        double value3 = (double) 1 / (double) paramList.get(3);
        //计算
        return (long) Math.pow(value1 * value2, value3) + (long) paramList.get(6);
    }

    /************************************交互方法**************************************************/

    /**
     * 进入活动
     * @param reqMsg
     * @throws BusinessException
     */
    public void enterActivity(HsIslandProto.HsIslandEnterActivityReqMsg reqMsg) throws BusinessException {
        HsIslandProto.HsIslandEnterActivityCrossReqMsg.Builder reqMsgBuilder = HsIslandProto.HsIslandEnterActivityCrossReqMsg.newBuilder();
        reqMsgBuilder.setActivityId(reqMsg.getActivityId());
        //公用check
        this.commonCheck(reqMsg.getActivityId(), true);
        //获取玩家baseInfo
        UserBaseInfo userBaseInfo = UserMgr.parseUserBaseInfo(player.getUserInfo(), GameServer.getInstance().getServerId());
        if (userBaseInfo == null) {
            throw new BusinessException(GameErrorCode.E_HS_ISLAND_USER_BASE_NOT_FOUND);
        }
//        //判断玩家是否计入商会
//        if (StringUtils.isNullOrEmpty(userBaseInfo.getUnionUid())) {
//            throw new BusinessException(GameErrorCode.E_HS_ISLAND_USER_NOT_UNION);
//        }
        reqMsgBuilder.setUserInfo(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
        //获取前100战力门客
        List<HsIslandPatronsData> partonsList = this.getSortPatronsList(reqMsg.getActivityId());
        for (HsIslandPatronsData patronsData : partonsList) {
            HsIslandProto.HsIslandPatronsData.Builder patronsDataBuilder = HsIslandProto.HsIslandPatronsData.newBuilder();
            patronsDataBuilder.setPatronsId(patronsData.getPatronsId());
            patronsDataBuilder.setBasePower(patronsData.getPower());
            reqMsgBuilder.addPatrons(patronsDataBuilder);
        }
        //上跨服
        player.sendUnionActivityGroupPacket(Protocol.C2_HS_ISLAND_ENTER_ACTIVITY, reqMsgBuilder, reqMsgBuilder.getActivityId());
    }

    /**
     * 事件处理-2选1
     * @param reqMsg
     * @throws BusinessException
     */
    public void doChoose(HsIslandProto.HsIslandEventChooseRewardReqMsg reqMsg) throws BusinessException {
        //公用check
        this.commonCheck(reqMsg.getActivityId(), true);
        //上跨服
        player.sendUnionActivityGroupPacket(Protocol.C2_HS_ISLAND_DO_EVENT_CHOOSE_REWARD, reqMsg.toBuilder(), reqMsg.getActivityId());
    }

    /**
     * 事件处理-明灯
     * @param reqMsg
     * @throws BusinessException
     */
    public void doLight(HsIslandProto.HsIslandEventLightReqMsg reqMsg) throws BusinessException {
        //公用check
        this.commonCheck(reqMsg.getActivityId(), true);
        //上跨服
        player.sendUnionActivityGroupPacket(Protocol.C2_HS_ISLAND_DO_EVENT_LIGHT, reqMsg.toBuilder(), reqMsg.getActivityId());
    }

    /**
     * 捐献
     * @param reqMsg
     * @throws BusinessException
     */
    public void donate(HsIslandProto.HsIslandDonateReqMsg reqMsg) throws BusinessException {
        //公用check
        this.commonCheck(reqMsg.getActivityId(), false);
        HsIslandConfig config = HsIslandActivityMgr.getActivityConfig(reqMsg.getActivityId());
        //道具
        int propId = config.HSISLAND_DONATE_ITEM_ID;
        Property cost = new Property(propId, reqMsg.getPropNum());
        //判断道具是否足够
        if (!player.getModule(CurrencyModule.class).currencyIsEnough(cost)) {
            throw BusinessException.newException(GameErrorCode.E_HS_ISLAND_PROP_NOT_ENOUGH);
        }
        //预扣除
        player.getModule(PreReduceModule.class).preReduce(cost);
        //上跨服
        player.sendUnionActivityGroupPacket(Protocol.C2_HS_ISLAND_DONATE, reqMsg.toBuilder(), reqMsg.getActivityId());
    }

    /**
     * 事件处理-开宝箱
     * @param reqMsg
     * @throws BusinessException
     */
    public void doOpenBox(HsIslandProto.HsIslandEventOpenBoxReqMsg reqMsg) throws BusinessException {
        //公用check
        this.commonCheck(reqMsg.getActivityId(), true);
        //上跨服
        player.sendUnionActivityGroupPacket(Protocol.C2_HS_ISLAND_OPEN_BOX, reqMsg.toBuilder(), reqMsg.getActivityId());
    }

    /**
     * 事件处理-遗迹藏宝
     * @param reqMsg
     * @throws BusinessException
     */
    public void doRelic(HsIslandProto.HsIslandEventRelicReqMsg reqMsg) throws BusinessException {
        //公用check
        this.commonCheck(reqMsg.getActivityId(), true);
        //上跨服
        player.sendUnionActivityGroupPacket(Protocol.C2_HS_ISLAND_DO_EVENT_RELIC, reqMsg.toBuilder(), reqMsg.getActivityId());
    }

    /**
     * 进入下一海域
     * @param reqMsg
     * @throws BusinessException
     */
    public void enterFloor(HsIslandProto.HsIslandEnterFloorReqMsg reqMsg) throws BusinessException {
        //公用check
        this.commonCheck(reqMsg.getActivityId(), true);
        //上跨服
        player.sendUnionActivityGroupPacket(Protocol.C2_HS_ISLAND_ENTER_FLOOR, reqMsg.toBuilder(), reqMsg.getActivityId());
    }

    /**
     * 探索
     * @param reqMsg
     * @throws BusinessException
     */
    public void explore(HsIslandProto.HsIslandExploreReqMsg reqMsg) throws BusinessException {
        //公用check
        this.commonCheck(reqMsg.getActivityId(), true);
        //上跨服
        player.sendUnionActivityGroupPacket(Protocol.C2_HS_ISLAND_EXPLORE, reqMsg.toBuilder(), reqMsg.getActivityId());
    }

    /**
     * 批量探索
     * @param reqMsg
     * @throws BusinessException
     */
    public void exploreBatch(HsIslandProto.HsIslandExploreBatchReqMsg reqMsg) throws BusinessException {
        //公用check
        this.commonCheck(reqMsg.getActivityId(), true);
        //上跨服
        player.sendUnionActivityGroupPacket(Protocol.C2_HS_ISLAND_EXPLORE_BATCH, reqMsg.toBuilder(), reqMsg.getActivityId());
    }

    /**
     * 商会情报-海神宝库列表
     * @param reqMsg
     * @throws BusinessException
     */
    public void getFloorBoxList(HsIslandProto.HsIslandFloorBoxListReqMsg reqMsg) throws BusinessException {
        //公用check
        this.commonCheck(reqMsg.getActivityId(), true);
        //上跨服
        player.sendUnionActivityGroupPacket(Protocol.C2_HS_ISLAND_FLOOR_BOX_LIST, reqMsg.toBuilder(), reqMsg.getActivityId());
    }

    /**
     * 商会情报-协助列表
     * @param reqMsg
     * @throws BusinessException
     */
    public void getHelpLogList(HsIslandProto.HsIslandHelpLogListReqMsg reqMsg) throws BusinessException {
        //公用check
        this.commonCheck(reqMsg.getActivityId(), true);
        //上跨服
        player.sendUnionActivityGroupPacket(Protocol.C2_HS_ISLAND_HELP_LOG_LIST, reqMsg.toBuilder(), reqMsg.getActivityId());
    }

    /**
     * 移动
     * @param reqMsg
     * @throws BusinessException
     */
    public void move(HsIslandProto.HsIslandMoveReqMsg reqMsg) throws BusinessException {
        //公用check
        this.commonCheck(reqMsg.getActivityId(), true);
        //上跨服
        player.sendUnionActivityGroupPacket(Protocol.C2_HS_ISLAND_MOVE, reqMsg.toBuilder(), reqMsg.getActivityId());
    }

    /**
     * 事件处理-pvp
     * @param reqMsg
     * @throws BusinessException
     */
    public void pvp(HsIslandProto.HsIslandEventPvpBattleReqMsg reqMsg) throws BusinessException {
        //公用check
        this.commonCheck(reqMsg.getActivityId(), true);
        //上跨服
        player.sendUnionActivityGroupPacket(Protocol.C2_HS_ISLAND_DO_EVENT_PVP, reqMsg.toBuilder(), reqMsg.getActivityId());
    }

    /**
     * 商会情报-领取【商会情报-海神宝库】
     * @param reqMsg
     * @throws BusinessException
     */
    public void receiveFloorBoxReward(HsIslandProto.HsIslandFloorBoxReceiveReqMsg reqMsg) throws BusinessException {
        //公用check
        this.commonCheck(reqMsg.getActivityId(), true);
        //上跨服
        player.sendUnionActivityGroupPacket(Protocol.C2_HS_ISLAND_FLOOR_BOX_RECEIVE, reqMsg.toBuilder(), reqMsg.getActivityId());
    }

    /**
     * 商会情报-领取【协助奖励】
     * @param reqMsg
     * @throws BusinessException
     */
    public void receiveHelpLogReward(HsIslandProto.HsIslandHelpLogReceiveReqMsg reqMsg) throws BusinessException {
        //公用check
        this.commonCheck(reqMsg.getActivityId(), true);
        //上跨服
        player.sendUnionActivityGroupPacket(Protocol.C2_HS_ISLAND_HELP_LOG_RECEIVE, reqMsg.toBuilder(), reqMsg.getActivityId());
    }

    /**
     * 盟友列表
     * @param reqMsg
     * @throws BusinessException
     */
    public void getUnionUserList(HsIslandProto.HsIslandUnionUserListReqMsg reqMsg) throws BusinessException {
        //公用check
        this.commonCheck(reqMsg.getActivityId(), false);
        //上跨服
        player.sendUnionActivityGroupPacket(Protocol.C2_HS_ISLAND_UNION_USER_LIST, reqMsg.toBuilder(), reqMsg.getActivityId());
    }

    /**
     * 解锁打理形象
     * @param reqMsg
     * @throws BusinessException
     */
    public void unlockAutoImage(HsIslandProto.HsIslandUnlockAutoImageReqMsg reqMsg) throws BusinessException {
        //公用check
        this.commonCheck(reqMsg.getActivityId(), true);
        //消耗的道具
        HsIslandConfig config = HsIslandActivityMgr.getActivityConfig(reqMsg.getActivityId());
        Property cost = config.HSISLAND_FAST_FUNCTION_OPEN_CONSUME;
        //判断道具是否足够
        if (!player.getModule(CurrencyModule.class).currencyIsEnough(cost)) {
            throw BusinessException.newException(GameErrorCode.E_HS_ISLAND_PROP_NOT_ENOUGH);
        }
        //预扣除
        player.getModule(PreReduceModule.class).preReduce(cost);
        //上跨服
        player.sendUnionActivityGroupPacket(Protocol.C2_HS_ISLAND_UNLOCK_AUTO_IMAGE, reqMsg.toBuilder(), reqMsg.getActivityId());
    }

    /**
     * 穿戴形象
     * @param reqMsg
     * @throws BusinessException
     */
    public void useImage(HsIslandProto.HsIslandUseImageReqMsg reqMsg) throws BusinessException {
        //公用check
        this.commonCheck(reqMsg.getActivityId(), true);
        //上跨服
        player.sendUnionActivityGroupPacket(Protocol.C2_HS_ISLAND_USE_IMAGE, reqMsg.toBuilder(), reqMsg.getActivityId());
    }

    /**
     * 打理奖励列表
     * @param reqMsg
     * @throws BusinessException
     */
    public void getAutoReward(HsIslandProto.HsIslandAutoRewardReqMsg reqMsg) throws BusinessException {
        //公用check
        this.commonCheck(reqMsg.getActivityId(), true);
        //上跨服
        player.sendUnionActivityGroupPacket(Protocol.C2_HS_ISLAND_AUTO_REWARD_LST, reqMsg.toBuilder(), reqMsg.getActivityId());
    }

    /**
     * 清空打理奖励列表
     * @param reqMsg
     * @throws BusinessException
     */
    public void clearAutoReward(HsIslandProto.HsIslandClearAutoRewardReqMsg reqMsg) throws BusinessException {
        //公用check
        this.commonCheck(reqMsg.getActivityId(), true);
        //上跨服
        player.sendUnionActivityGroupPacket(Protocol.C2_HS_ISLAND_CLEAR_AUTO_REWARD, reqMsg.toBuilder(), reqMsg.getActivityId());
    }

    /**
     * 商会日志
     * @param reqMsg
     * @throws BusinessException
     */
    public void getUnionLogList(HsIslandProto.HsIslandUnionLogReqMsg reqMsg) throws BusinessException {
        //公用check
        this.commonCheck(reqMsg.getActivityId(), true);
        //上跨服
        player.sendUnionActivityGroupPacket(Protocol.C2_HS_ISLAND_UNION_LOG, reqMsg.toBuilder(), reqMsg.getActivityId());
    }

    /**
     * 玩家日志
     * @param reqMsg
     * @throws BusinessException
     */
    public void getUserLogList(HsIslandProto.HsIslandUserLogReqMsg reqMsg) throws BusinessException {
        //公用check
        this.commonCheck(reqMsg.getActivityId(), true);
        //上跨服
        player.sendUnionActivityGroupPacket(Protocol.C2_HS_ISLAND_USER_LOG, reqMsg.toBuilder(), reqMsg.getActivityId());
    }

    /**
     * 获取岛屿列表
     * @param reqMsg
     * @throws BusinessException
     */
    public void getIslandList(HsIslandProto.HsIslandGetIslandListReqMsg reqMsg) throws BusinessException {
        //公用check
        this.commonCheck(reqMsg.getActivityId(), true);
        //上跨服
        player.sendUnionActivityGroupPacket(Protocol.C2_HS_ISLAND_GET_ISLAND_LIST, reqMsg.toBuilder(), reqMsg.getActivityId());
    }

    /**
     * 使用道具恢复体力
     * @param reqMsg
     * @throws BusinessException
     */
    public void usePropRecoverEnergy(HsIslandProto.HsIslandUsePropRecoverEnergyReqMsg reqMsg) throws BusinessException {
        //公用check
        this.commonCheck(reqMsg.getActivityId(), true);
        //消耗的道具
        HsIslandConfig config = HsIslandActivityMgr.getActivityConfig(reqMsg.getActivityId());
        Property cost = new Property(config.HSISLAND_ENERGY_ITEM_ID, reqMsg.getNum());
        //判断道具是否足够
        if (!player.getModule(CurrencyModule.class).currencyIsEnough(cost)) {
            throw BusinessException.newException(GameErrorCode.E_HS_ISLAND_PROP_NOT_ENOUGH);
        }
        //预扣除
        player.getModule(PreReduceModule.class).preReduce(cost);
        //上跨服
        player.sendUnionActivityGroupPacket(Protocol.C2_HS_ISLAND_USE_PROP_RECOVER_ENERGY, reqMsg.toBuilder(), reqMsg.getActivityId());
    }

    /**
     * 解锁形象，补发积分
     */
    public void unlockScoreImageBfScore() {
        Map<Integer, HsIslandConfig> activityConfigMap = HsIslandActivityMgr.getActivityConfigMap();
        for (HsIslandConfig config : activityConfigMap.values()) {
            int activityId = config.getActivityInfo().getActivityId();
            //公用check
            try {
                this.commonCheck(activityId, false);
                //获取玩家baseInfo
                UserBaseInfo userBaseInfo = UserMgr.parseUserBaseInfo(player.getUserInfo(), GameServer.getInstance().getServerId());
                if (userBaseInfo == null) {
                    throw new BusinessException(GameErrorCode.E_HS_ISLAND_USER_BASE_NOT_FOUND);
                }
                //上跨服
                HsIslandProto.HsIslandUnlockImageBfScoreCrossReqMsg.Builder reqMsg = HsIslandProto.HsIslandUnlockImageBfScoreCrossReqMsg.newBuilder();
                reqMsg.setActivityId(activityId);
                reqMsg.setUserInfo(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
                player.sendUnionActivityGroupPacket(Protocol.C2_HS_ISLAND_UNLOCK_IMAGE_BF_SCORE, reqMsg, activityId);
            } catch (BusinessException e) {
                logger.error("海神岛-解锁形象，补发积分失败:{},{},{}", getUserId(), activityId, e.getCode());
            }
        }
    }

    /**
     * 进入战场
     * @param reqMsg
     * @throws BusinessException
     */
    public void enterBattleField(HsIslandProto.HsIslandEnterBattleFieldReqMsg reqMsg) throws BusinessException {
        HsIslandProto.HsIslandEnterBattleFieldReqMsg.Builder reqMsgBuilder = reqMsg.toBuilder();
        //公用check
        this.commonCheck(reqMsg.getActivityId(), true);
        //上跨服
        player.sendUnionActivityGroupPacket(Protocol.C2_HS_ISLAND_ENTER_BATTLE_FIELD, reqMsgBuilder, reqMsgBuilder.getActivityId());
    }

    /**
     * 离开战场
     *
     * @param reqMsg
     */
    public void leaveBattleField(HsIslandProto.HsIslandLeaveBattleFieldReqMsg reqMsg) throws BusinessException {
        HsIslandProto.HsIslandLeaveBattleFieldReqMsg.Builder reqMsgBuilder = reqMsg.toBuilder();
        //公用check
        this.commonCheck(reqMsg.getActivityId(), true);
        //上跨服
        player.sendUnionActivityGroupPacket(Protocol.C2_HS_ISLAND_LEAVE_BATTLE_FIELD, reqMsgBuilder, reqMsgBuilder.getActivityId());
    }


    /**
     * 战场操作
     *
     * @param reqMsg
     */
    public void battleFieldOperate(HsIslandProto.HsIslandBattleFieldOperateReqMsg reqMsg) throws BusinessException {
        HsIslandProto.HsIslandBattleFieldOperateReqMsg.Builder reqMsgBuilder = reqMsg.toBuilder();
        //公用check
        this.commonCheck(reqMsg.getActivityId(), true);
        //上跨服
        player.sendUnionActivityGroupPacket(Protocol.C2_HS_ISLAND_BATTLE_FIELD_OPERATE, reqMsgBuilder, reqMsgBuilder.getActivityId());
    }


    public void battleFieldDataReq(HsIslandProto.HsIslandBattleFieldDataReqMsg reqMsg) throws BusinessException {
        HsIslandProto.HsIslandBattleFieldDataReqMsg.Builder reqMsgBuilder = reqMsg.toBuilder();
        //公用check
        this.commonCheck(reqMsg.getActivityId(), true);
        //上跨服
        player.sendUnionActivityGroupPacket(Protocol.C2_HS_ISLAND_BATTLE_FIELD_DATA_REQ, reqMsgBuilder, reqMsgBuilder.getActivityId());
    }

    /**
     * 战场商会玩家LIST
     * @param reqMsg
     * @throws BusinessException
     */
    public void getBattleFieldUnionUserList(HsIslandProto.HsIslandBattleFieldUnionUserListReqMsg reqMsg) throws BusinessException {
        HsIslandProto.HsIslandBattleFieldUnionUserListReqMsg.Builder reqMsgBuilder = reqMsg.toBuilder();
        //公用check
        this.commonCheck(reqMsg.getActivityId(), true);
        //上跨服
        player.sendUnionActivityGroupPacket(Protocol.C2_HS_ISLAND_GET_BATTLE_FIELD_UNION_USER_LIST, reqMsgBuilder, reqMsgBuilder.getActivityId());
    }

    public void killBossDamageRankReq(HsIslandProto.HsIslandKillBossDamageRankReqMsg reqMsg) throws BusinessException {
        HsIslandProto.HsIslandKillBossDamageRankReqMsg.Builder reqMsgBuilder = reqMsg.toBuilder();
        //公用check
        this.commonCheck(reqMsg.getActivityId(), false);
        //上跨服
        player.sendUnionActivityGroupPacket(Protocol.C2_HS_ISLAND_KILL_BOSS_DAMAGE_RANK_REQ, reqMsgBuilder, reqMsgBuilder.getActivityId());
    }

    public void killBossDamageRewardReq(HsIslandProto.HsIslandKillBossDamageRankRewardReqMsg reqMsg) throws BusinessException {
        HsIslandProto.HsIslandKillBossDamageRankRewardReqMsg.Builder reqMsgBuilder = reqMsg.toBuilder();
        //公用check
        this.commonCheck(reqMsg.getActivityId(), false);
        //上跨服
        player.sendUnionActivityGroupPacket(Protocol.C2_HS_ISLAND_KILL_BOSS_DAMAGE_RANK_REWARD_REQ, reqMsgBuilder, reqMsgBuilder.getActivityId());
    }

    /*********************************跨服返回******************************************************/

    /**
     * 跨服返回-探索
     * @param respMsg
     */
    public void exploreFromCross(HsIslandProto.HsIslandExploreRespMsg respMsg) {
        if (respMsg.hasExploreResult() && respMsg.getExploreResult().hasRewardData()) {
            String rewardStr = respMsg.getExploreResult().getRewardData().getReward();
            if (!StringUtils.isNullOrEmpty(rewardStr)) {
                Property reward = PropertyHelper.parseStringToProperty(rewardStr);
                //添加奖励
                player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.HsIsland, eLogMoneyType.HsIslandExploreReward);
            }
        }
        //回包
        player.sendPacket(Protocol.U_HS_ISLAND_EXPLORE, respMsg.toBuilder());
    }

    /**
     * 跨服返回-批量探索
     * @param respMsg
     */
    public void exploreBatchFromCross(HsIslandProto.HsIslandExploreBatchRespMsg respMsg) {
        for (HsIslandProto.HsIslandExploreResult exploreResult : respMsg.getExploreResultList()) {
            if (exploreResult.hasRewardData()) {
                String rewardStr = exploreResult.getRewardData().getReward();
                if (!StringUtils.isNullOrEmpty(rewardStr)) {
                    Property reward = PropertyHelper.parseStringToProperty(rewardStr);
                    //添加奖励
                    player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.HsIsland, eLogMoneyType.HsIslandExploreReward);
                }
            }
        }
        //回包
        player.sendPacket(Protocol.U_HS_ISLAND_EXPLORE_BATCH, respMsg.toBuilder());
    }

    /**
     * 跨服返回-pvp
     * @param respMsg
     */
    public void pvpFromCross(HsIslandProto.HsIslandEventPvpBattleRespMsg respMsg) {
        if (respMsg.hasBattleResult() && respMsg.getBattleResult().hasRewardData()) {
            String rewardStr = respMsg.getBattleResult().getRewardData().getReward();
            if (!StringUtils.isNullOrEmpty(rewardStr)) {
                Property reward = PropertyHelper.parseStringToProperty(rewardStr);
                //连胜奖励
                String moreRewardStr = respMsg.getBattleResult().getRewardData().getMoreWinReward();
                if (!StringUtils.isNullOrEmpty(moreRewardStr)) {
                    reward.addProperty(PropertyHelper.parseStringToProperty(moreRewardStr));
                }
                //添加奖励
                player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.HsIsland, eLogMoneyType.HsIslandPvpReward);
            }
        }
        //回包
        player.sendPacket(Protocol.U_HS_ISLAND_DO_EVENT_PVP, respMsg.toBuilder());
    }

    /**
     * 跨服返回-2选1
     * @param respMsg
     */
    public void doChooseFromCross(HsIslandProto.HsIslandEventChooseRewardRespMsg respMsg) {
        if (respMsg.hasRewardData()) {
            String rewardStr = respMsg.getRewardData().getReward();
            if (!StringUtils.isNullOrEmpty(rewardStr)) {
                Property reward = PropertyHelper.parseStringToProperty(rewardStr);
                //添加奖励
                player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.HsIsland, eLogMoneyType.HsIslandChooseReward);
            }
        }
        //判断是否选择了正确选项
        HsIslandConfig config = HsIslandActivityMgr.getActivityConfig(respMsg.getActivityId());
        if (config != null && respMsg.hasChooseConfigId() && respMsg.getChooseConfigId() > 0) {
            if (respMsg.hasChooseTrue() && respMsg.getChooseTrue()) {
                player.getModule(UserActModule.class).addActTrueChooseIndex(
                        config.getActivityInfo().getType(),
                        config.getActivityInfo().getChildType(),
                        String.valueOf(respMsg.getChooseConfigId()),
                        String.valueOf(respMsg.getIndex()));
            }
        }
        //回包
        player.sendPacket(Protocol.U_HS_ISLAND_DO_EVENT_CHOOSE_REWARD, respMsg.toBuilder());
    }

    /**
     * 跨服返回-遗迹藏宝
     * @param respMsg
     */
    public void doRelicFromCross(HsIslandProto.HsIslandEventRelicRespMsg respMsg) {
        if (respMsg.hasRewardData()) {
            String rewardStr = respMsg.getRewardData().getReward();
            if (!StringUtils.isNullOrEmpty(rewardStr)) {
                Property reward = PropertyHelper.parseStringToProperty(rewardStr);
                //添加奖励
                player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.HsIsland, eLogMoneyType.HsIslandRelicReward);
            }
        }
        //回包
        player.sendPacket(Protocol.U_HS_ISLAND_DO_EVENT_RELIC, respMsg.toBuilder());
    }

    /**
     * 跨服返回-开启宝箱
     * @param respMsg
     */
    public void doOpenBoxFromCross(HsIslandProto.HsIslandEventOpenBoxRespMsg respMsg) {
        if (respMsg.hasRewardData()) {
            String rewardStr = respMsg.getRewardData().getReward();
            if (!StringUtils.isNullOrEmpty(rewardStr)) {
                Property reward = PropertyHelper.parseStringToProperty(rewardStr);
                //添加奖励
                player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.HsIsland, eLogMoneyType.HsIslandOpenBoxReward);
            }
        }
        //回包
        player.sendPacket(Protocol.U_HS_ISLAND_OPEN_BOX, respMsg.toBuilder());
    }

    /**
     * 跨服返回-明灯
     * @param respMsg
     */
    public void doLightFromCross(HsIslandProto.HsIslandEventLightRespMsg respMsg) {
        for (HsIslandProto.HsIslandExploreResult exploreResult : respMsg.getLightExploreResultList()) {
            if (exploreResult.hasRewardData()) {
                String rewardStr = exploreResult.getRewardData().getReward();
                if (!StringUtils.isNullOrEmpty(rewardStr)) {
                    Property reward = PropertyHelper.parseStringToProperty(rewardStr);
                    //添加奖励
                    player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.HsIsland, eLogMoneyType.HsIslandLightReward);
                }
            }
        }
        //回包
        player.sendPacket(Protocol.U_HS_ISLAND_DO_EVENT_LIGHT, respMsg.toBuilder());
    }

    /**
     * 跨服返回-领取【商会情报-事件奖励】
     * @param respMsg
     */
    public void receiveHelpLogFromCross(HsIslandProto.HsIslandHelpLogReceiveRespMsg respMsg) {
        if (respMsg.hasRewardData()) {
            String rewardStr = respMsg.getRewardData().getReward();
            if (!StringUtils.isNullOrEmpty(rewardStr)) {
                Property reward = PropertyHelper.parseStringToProperty(rewardStr);
                //添加奖励
                player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.HsIsland, eLogMoneyType.HsIslandReceiveHelpLogReward);
            }
        }
        //回包
        player.sendPacket(Protocol.U_HS_ISLAND_HELP_LOG_RECEIVE, respMsg.toBuilder());
    }

    /**
     * 跨服返回-领取【商会情报-海神宝库】
     * @param respMsg
     */
    public void receiveFloorBoxFromCross(HsIslandProto.HsIslandFloorBoxReceiveRespMsg respMsg) {
        if (respMsg.hasRewardData()) {
            String rewardStr = respMsg.getRewardData().getReward();
            if (!StringUtils.isNullOrEmpty(rewardStr)) {
                Property reward = PropertyHelper.parseStringToProperty(rewardStr);
                //添加奖励
                player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.HsIsland, eLogMoneyType.HsIslandReceiveFloorBoxReward);
            }
        }
        //回包
        player.sendPacket(Protocol.U_HS_ISLAND_FLOOR_BOX_RECEIVE, respMsg.toBuilder());
    }

    /**
     * 跨服返回-解锁打理形象
     * @param respMsg
     */
    public void unlockAutoImageFromCross(HsIslandProto.HsIslandUnlockAutoImageRespMsg respMsg) {
        //消耗的道具
        HsIslandConfig config = HsIslandActivityMgr.getActivityConfig(respMsg.getActivityId());
        if (config != null) {
            Property cost = config.HSISLAND_FAST_FUNCTION_OPEN_CONSUME;
            //清理预扣除
            player.getModule(PreReduceModule.class).restorePreReduce(cost);
            if (respMsg.getRet() == 0) {
                //扣除道具
                player.getModule(CurrencyModule.class).removeCurrency(cost, eLogMoneyType.HsIsland, eLogMoneyType.HsIslandUnlockAutoImageCost);
            }
        }
        //回包
        player.sendPacket(Protocol.U_HS_ISLAND_UNLOCK_AUTO_IMAGE, respMsg.toBuilder());
    }

    /**
     * 跨服返回-捐献
     * @param respMsg
     */
    public void donateFromCross(HsIslandProto.HsIslandDonateRespMsg respMsg) {
        HsIslandConfig config = HsIslandActivityMgr.getActivityConfig(respMsg.getActivityId());
        if (config != null) {
            //消耗的道具
            int propId = config.HSISLAND_DONATE_ITEM_ID;
            Property cost = new Property(propId, respMsg.getPropNum());
            //清理预扣除
            player.getModule(PreReduceModule.class).restorePreReduce(cost);
            if (respMsg.getRet() == 0) {
                //扣除道具
                player.getModule(CurrencyModule.class).removeCurrency(cost, eLogMoneyType.HsIsland, eLogMoneyType.HsIslandDonateCost);
                //奖励
                if (respMsg.hasReward()) {
                    String rewardStr = respMsg.getReward();
                    if (!StringUtils.isNullOrEmpty(rewardStr)) {
                        Property reward = PropertyHelper.parseStringToProperty(rewardStr);
                        //添加奖励
                        player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.HsIsland, eLogMoneyType.HsIslandDonateReward);
                    }
                }
            }
        }
        //回包
        player.sendPacket(Protocol.U_HS_ISLAND_DONATE, respMsg.toBuilder());
    }

    /**
     * 跨服返回-使用道具恢复体力
     * @param respMsg
     */
    public void usePropRecoverEnergyFromCross(HsIslandProto.HsIslandUsePropRecoverEnergyRespMsg respMsg) {
        HsIslandConfig config = HsIslandActivityMgr.getActivityConfig(respMsg.getActivityId());
        if (config != null) {
            //消耗的道具
            int propId = config.HSISLAND_ENERGY_ITEM_ID;
            Property cost = new Property(propId, respMsg.getNum());
            //清理预扣除
            player.getModule(PreReduceModule.class).restorePreReduce(cost);
            if (respMsg.getRet() == 0) {
                //扣除道具
                player.getModule(CurrencyModule.class).removeCurrency(cost, eLogMoneyType.HsIsland, eLogMoneyType.HsIslandRecoverEnergyCost);
            }
        }
        //回包
        player.sendPacket(Protocol.U_HS_ISLAND_USE_PROP_RECOVER_ENERGY, respMsg.toBuilder());
    }

    /**
     * 跨服返回-战场操作
     * @param respMsg
     */
    public void battleFieldOperateFromCross(HsIslandProto.HsIslandBattleFieldOperateRespMsg respMsg) {
        if (respMsg.hasRewardData()) {
            String rewardStr = respMsg.getRewardData().getReward();
            if (!StringUtils.isNullOrEmpty(rewardStr)) {
                Property reward = PropertyHelper.parseStringToProperty(rewardStr);
                //添加奖励
                player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.HsIsland, eLogMoneyType.HsIslandOpBattleReward);
            }
        }
        //回包
        player.sendPacket(Protocol.U_HS_ISLAND_BATTLE_FIELD_OPERATE, respMsg.toBuilder());
    }

    /**
     * 玩家心跳断开后的操作
     */
    public void offLine() {
        if (!SystemOpenMgr.systemOpen(player, eSystemId.HsIsland.getValue())) {
            return;
        }

        for (HsIslandConfig config : HsIslandActivityMgr.getActivityConfigMap().values()) {
            if (!ActivityHelper.activityInShowTime(config.getActivityInfo())) {
                continue;
            }

            // 需要退出战场
            HsIslandProto.HsIslandLeaveBattleFieldReqMsg.Builder reqMsg = HsIslandProto.HsIslandLeaveBattleFieldReqMsg.newBuilder();
            reqMsg.setActivityId(config.getActivityInfo().getActivityId());
            //上跨服
            player.sendUnionActivityGroupPacket(Protocol.C2_HS_ISLAND_LEAVE_BATTLE_FIELD, reqMsg, config.getActivityInfo().getActivityId());
        }
    }
}
